package com.atguigu.gulimall.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * PDF模版工具包
 * @author 王鸿远
 *
 */
@Slf4j
public class PdfTemplateUtils {

//    private static BaseFont bfChinese;//宋体
//    private static BaseFont bfmsyhChinese;//微软雅黑体
//    private static BaseFont bfmsjhChinese;//微软正黑体
    private static BaseFont bfEnglish;//Arial
    private static BaseFont wingdings2;//truetype符号字体
//    private static BaseFont bfsyChinese;//思源黑体
    private static BaseFont simhei;// 中易黑體

    static {
        try {
//            bfChinese = BaseFont.createFont("/fonts/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//            bfmsyhChinese = BaseFont.createFont("/fonts/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//            bfmsjhChinese = BaseFont.createFont("/fonts/msjh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            wingdings2 = BaseFont.createFont("/fonts/Wingdings2.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//            bfsyChinese = BaseFont.createFont("/fonts/SourceHanSansCN-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            bfEnglish = BaseFont.createFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            simhei = BaseFont.createFont("/fonts/SimHei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException | IOException e) {
            log.error("error", e);
        }
    }
    // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
//    public static final Font eightfont = new Font(bfChinese, 8, Font.BOLD);
    public static final Font eightfont_en = new Font(bfEnglish, 8, Font.BOLD);
//    public static final Font ninefont = new Font(bfChinese, 9, Font.BOLD);
    public static final Font ninefont_en = new Font(bfEnglish, 9, Font.BOLD);
//    public static final Font tenfont = new Font(bfChinese, 10, Font.BOLD);
//    public static final Font tenfontNormal = new Font(bfChinese, 10, Font.NORMAL);
//    public static final Font tenfont_sy = new Font(bfsyChinese, 10, Font.BOLD);
//    public static final Font tenfont_blue = new Font(bfChinese, 10, Font.BOLD,BaseColor.BLUE);
    public static final Font tenfont_en = new Font(bfEnglish, 10, Font.BOLD);
    public static final Font tenfont_en_blue = new Font(bfEnglish, 10f, Font.BOLD,BaseColor.BLUE);
//    public static final Font tenfontNormal_sy = new Font(bfsyChinese, 10, Font.NORMAL);
    public static final Font tenfontNormal_en = new Font(bfEnglish, 10, Font.NORMAL);
//    public static final Font eightfontNormal = new Font(bfChinese, 8, Font.NORMAL);
//    public static final Font eightfontBlod = new Font(bfChinese, 8f, Font.BOLD);
//    public static final Font eightfontNormal_blue = new Font(bfChinese, 8, Font.NORMAL,BaseColor.BLUE);
    public static final Font eightfontNormal_en = new Font(bfEnglish, 8, Font.NORMAL);
    public static final Font eightfontBlod_en = new Font(bfEnglish, 8f, Font.BOLD);
    public static final Font eightfontNormal_en_blue = new Font(bfEnglish, 8, Font.NORMAL,BaseColor.BLUE);
//    public static final Font underLineFont_eightfontBold = new Font(bfChinese, 8, Font.UNDERLINE | Font.BOLD);
    public static final Font underLineFont_eightfontBold_en = new Font(bfEnglish, 8, Font.UNDERLINE | Font.BOLD);
    public static final Font headfont_english = new Font(bfEnglish, 12.5f, Font.BOLD);
    public static final Font headfont_english_blue = new Font(bfEnglish, 12.5f, Font.BOLD,BaseColor.BLUE);
//    public static final Font headfont = new Font(bfChinese, 12.5f, Font.BOLD);
//    public static final Font keyfont = new Font(bfChinese, 8f, Font.BOLD);
//    public static final Font textfont = new Font(bfChinese, 8f, Font.NORMAL);
//    public static final Font underLineFont = new Font(bfChinese, 8f, Font.UNDERLINE | Font.BOLD);
    public static final Font keyfont_english = new Font(bfEnglish, 8f, Font.BOLD);
    public static final Font textfont_english = new Font(bfEnglish, 8f, Font.NORMAL);
    public static final Font underLineFont_en = new Font(bfEnglish, 8f, Font.UNDERLINE | Font.BOLD);
//    public static final Font underLineFont_twelve = new Font(bfChinese, 12f, Font.UNDERLINE | Font.BOLD);
    public static final Font underLineFont_en_twelve = new Font(bfEnglish, 12f, Font.UNDERLINE | Font.BOLD);
//    public static final Font titleFont_twelve = new Font(bfChinese, 12f, Font.BOLD);
    public static final Font titleFont_en_twelve = new Font(bfEnglish, 12f, Font.BOLD);
    public static final Font textfont_seven_english = new Font(bfEnglish, 8f, Font.NORMAL);
//    public static final Font fourFont_italic = new Font(bfChinese, 6, Font.ITALIC);
    public static final Font fourFont_english_italic = new Font(bfEnglish, 6, Font.ITALIC);

//    public static final Font wingdings_point = new Font(wingdings2, 9, Font.NORMAL);
//    public static final Font eightfont_italic = new Font(bfChinese, 8, Font.ITALIC);
    public static final Font eightfont_english_italic = new Font(bfEnglish, 8, Font.ITALIC);
//    public static final Font sevenfont_italic = new Font(bfChinese, 7, Font.ITALIC);
    public static final Font sevenfont_english_italic = new Font(bfEnglish, 7, Font.ITALIC);

    public static final Font eightfont_underLineFont_en = new Font(bfEnglish, 8, Font.UNDERLINE | Font.NORMAL);

    public static final Font english_arial_normal_6f = new Font(bfEnglish, 6, Font.NORMAL);
    public static final Font english_arial_normal_7f = new Font(bfEnglish, 7, Font.NORMAL);
    public static final Font english_arial_normal_8f = new Font(bfEnglish, 8, Font.NORMAL);
    public static final Font english_arial_normal_10f = new Font(bfEnglish, 10, Font.NORMAL);
    public static final Font english_arial_normal_10f_line = new Font(bfEnglish, 10, Font.UNDERLINE | Font.NORMAL);
    public static final Font english_arial_BOLD_10f = new Font(bfEnglish, 10, Font.BOLD);
    public static final Font english_arial_BOLD_10f_line = new Font(bfEnglish, 10, Font.BOLD | Font.UNDERLINE);

    public static final Font english_arial_normal_12f = new Font(bfEnglish, 12, Font.NORMAL);
    public static final Font english_arial_BOLD_12f = new Font(bfEnglish, 12, Font.BOLD);
    public static final Font english_arial_BOLD_15f = new Font(bfEnglish, 15, Font.BOLD);
    public static final Font english_arial_normal_22f = new Font(bfEnglish, 22, Font.BOLD);

    //微软雅黑-中文
    public static final Font china_simhei_normal_10f = new Font(simhei, 10, Font.NORMAL);
    public static final Font china_simhei_normal_10f_line = new Font(simhei, 10, Font.UNDERLINE | Font.NORMAL);

    //特殊符號
    public static final Font wingdings_box = new Font(wingdings2, 12, Font.NORMAL);
    public static final Font wingdings_old = new Font(wingdings2, 9, Font.NORMAL);
    public static final Font wingdings_box_eight = new Font(wingdings2, 8, Font.NORMAL);
    public static final Font wingdings_point = new Font(wingdings2, 9, Font.NORMAL);
    public static final Font wingdings_twelve = new Font(wingdings2, 12, Font.NORMAL);
    public static final Font wingdings_bold = new Font(wingdings2, 12, Font.BOLD);


    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     *
     * @param phrase
     * @param align
     * @param colspan
     * @return
     */
    public static PdfPCell createCell(Phrase phrase, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_TOP);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(phrase);
        cell.setPaddingTop(-2);
        cell.setPaddingLeft(5);
        cell.setPaddingBottom(1);
        cell.setMinimumHeight(20);
        cell.setLeading(0, 1.2f);
        return cell;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(480);
            table.setLockedWidth(true);

            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            log.error("error", e);
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTablePremium(float[] widths, int with) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(with);
            table.setLockedWidth(true);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            log.error("error", e);
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTableNew(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(800);
            table.setLockedWidth(true);

            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            log.error("error", e);
        }
        return table;
    }


//    public static BaseFont getBfChinese() {
//        return bfChinese;
//    }
//
//    public static BaseFont getBfmsyhChinese() {
//        return bfmsyhChinese;
//    }
//
//    public static BaseFont getBfmsjhChinese() {
//        return bfmsjhChinese;
//    }

    public static BaseFont getBfEnglish() {
        return bfEnglish;
    }

    public static BaseFont getWingdings2() {
        return wingdings2;
    }

//    public static BaseFont getBfsyChinese() {
//        return bfsyChinese;
//    }

    public static BaseFont getSimhei() {
        return simhei;
    }
}
